import React from 'react';

import {Input} from 'antd';

import styles from './index.less';

export default class InputEx extends React.Component{

  state = {
    value:""
  }

  onValueChange(e){
    var v = e.target.value;

    var validate = this.props.validate;
    this.props.emitter.emit('onValueChange', this.props.name, v);
    this.setState({
      value:v
    });
  }

  iemi = null;

  reset(){
    this.props.emitter.emit('onValueChange', this.props.name, this.props.defaultValue);
    this.setState({
      value:this.props.defaultValue
    })
  }

  componentWillReceiveProps(n){
  }

  componentWillMount(){
    this.reset();

    this.iemi = this.props.emitter.addListener('onReset', ()=>{this.reset()});
    this.props.emitter.emit('onValueChange', this.props.name, this.props.defaultValue);
  }

  componentWillUnmount(){
    this.iemi.remove();
  }

  render(){
    return <Input name = {this.props.name} type = {this.props.type} autosize value = {this.state.value} onChange = {(e) => {
      this.onValueChange(e);
    }}/>
  }
}